﻿#region 命名空间

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI.WebControls;
using WinnerCMS.BLL;
using WinnerCMS.BLL.Shop;
using WinnerCMS.Common;
using WinnerCMS.Model;
using WinnerCMS.Model.Attribute;
using WinnerCMS.WebFactory;

#endregion

namespace WinnerCMS.WebUI.Admin.Shop
{
    [Access("ConfirmOrder,CancelOrder,DelOrder")]
    public partial class OrderList : PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ViewShowOrderItemDataBind(int.Parse(Request.QueryString["OrderId"]));
            }
        }

        protected void SwitchOrderListDataBindType()
        {
            //switch (hidOrderListDataBindType.Value.ToLower())
            //{
            //    case "quick":
            //        OrderListQuickSearch();
            //        break;
            //    case "advanced":
            //        OrderListAdancedSearch();
            //        break;
            //    default:
            //        GridOrderListDataBind();
            //        break;
            //}
        }

        #region  ViewShowOrderItem处理单个订单

        /// <summary>
        ///     订单详细信息视图数据绑定
        /// </summary>
        /// <param name="orderId"></param>
        protected void ViewShowOrderItemDataBind(int orderId)
        {
            var order = new OrdersManage();
            var model = order.GetInfo(orderId);
            if (model == null)
            {
                SwitchOrderListDataBindType();
                return;
            }

            this.lblOrderId.Text = model.Id.ToString();
            this.lblOrderNum.Text = model.OrderNum;
            this.lblInputTime.Text = model.CheckInDate.ToString();
            this.lblUserName2.Text = model.UserName;
            this.lblContacterName.Text = model.ContacterName;
            this.lblContacterName2.Text = model.ContacterName;
            this.lblAddress.Text = model.Address;
            this.lblZipCode.Text = model.ZipCode;
            this.lblPhone.Text = model.ZipCode;
            this.lblEmail.Text = model.Email;
            this.lblMobile.Text = model.Mobile;
            if (model.Invoiced)
                this.lblInvoiced.Text = "√";
            else
                this.lblInvoiced.Text = "×";

            this.lblCheckOutStatus.Text = order.GetMoneyReceivedStatusDescription(model.Id);

            this.lblDeliverStatus.Text = order.GetDeliverStatusDescription(model.Id);

            this.lblStatus.Text = order.GetOrderStatusDescription(model.Id);


            var paymentType = new PaymentMethodManage();
            var paymentTypeModel = paymentType.GetInfo(model.PaymentType);
            if (paymentTypeModel != null)
                this.lblPaymentType.Text = paymentTypeModel.MethodName;

            var deliverType = new DeliveryMethodManage();
            var deliverTypeModel = deliverType.GetInfo(model.DeliverType);
            if (deliverTypeModel != null)
                this.lblDeliverType.Text = deliverTypeModel.MethodName;


            this.lblPresentExp.Text = model.PresentExp.ToString();
            this.lblMoneyGoods.Text = string.Format("{0:c}", model.MoneyGoods);
            this.lblChargeDeliver.Text = string.Format("{0:c}", model.ChargeDeliver);

            if (model.Invoiced)
                this.lblNeedInvoice.Text = "√";
            else
                this.lblNeedInvoice.Text = "×";
            this.lblInvoiceContent.Text = HttpUtility.HtmlDecode(model.InvoiceContent);
            this.lblRemark.Text = HttpUtility.HtmlDecode(model.Remark);


            this.lblMoneyGoods2.Text = string.Format("{0:c}", model.MoneyGoods);
            this.lblChargeDeliver2.Text = string.Format("{0:c}", model.ChargeDeliver);
            this.lblChargeDeliver3.Text = string.Format("{0:c}", model.ChargeDeliver);
            this.lblTotal.Text = string.Format("{0:c}", model.MoneyTotal);
            this.lblTotal2.Text = string.Format("{0:c}", model.MoneyTotal);
            this.lblMoneyReceived.Text = string.Format("{0:c}", model.MoneyReceived);
            this.lblArrearage.Text = string.Format("{0:c}", model.MoneyTotal - model.MoneyReceived);

            var orderItem = new OrderListManage();

            this.gridShowOrderItem.DataSource = orderItem.GetList(model.Id);
            this.gridShowOrderItem.DataBind();

            //修改订单操作按钮的显示

            this.btnModifyOrderInfo.Visible = true;
            this.btnPreConfirmOrder.Visible = true;
            this.btnCancelConfirmOrder.Visible = true;
            this.btnDeleteOrder.Visible = true;
            this.btnRemitAndPay.Visible = true;
            this.btnAddPaymentorder.Visible = true;
            this.btnRefund.Visible = true;
            this.btnSendGoods.Visible = true;
            this.btnReceived.Visible = true;
            this.btnReturnGoods.Visible = true;
            this.btnEndOrder.Visible = true;
            this.btnInvoice.Visible = true;
            this.btnPrintOrder.Visible = true;


            //不需要发票
            if (!model.NeedInvoice)
            {
                this.btnInvoice.Visible = false;
            }
            //订单状态
            //假如已经结清
            if (model.Status == (int) Status.Finished)
            {
                this.btnModifyOrderInfo.Visible = false;
                this.btnPreConfirmOrder.Visible = false;
                this.btnCancelConfirmOrder.Visible = false;
                this.btnDeleteOrder.Visible = false;
                this.btnRemitAndPay.Visible = false;
                this.btnAddPaymentorder.Visible = false;
                this.btnSendGoods.Visible = false;
                this.btnReceived.Visible = false;
                this.btnEndOrder.Visible = false;
            }
            //假如等待确认 
            else if (model.Status == (int) Status.WaitForConfirm)
            {
                this.btnCancelConfirmOrder.Visible = false;
                this.btnRemitAndPay.Visible = false;
                this.btnAddPaymentorder.Visible = false;
                this.btnSendGoods.Visible = false;
                this.btnRefund.Visible = false;
                this.btnReceived.Visible = false;
                this.btnReturnGoods.Visible = false;
                this.btnEndOrder.Visible = false;
                this.btnInvoice.Visible = false;
            }
            //假如已经确认
            else
            {
                this.btnPreConfirmOrder.Visible = false;

                var moneyReceivedStatus = order.GetMoneyReceivedStatus(model.Id);

                if (model.DeliverStatus != (int) DeliverStatus.GoodsSending ||
                    model.DeliverStatus != (int) DeliverStatus.SiginIn ||
                    moneyReceivedStatus != MoneyReceivedStatus.WaitBuyerPay)
                    this.btnCancelConfirmOrder.Visible = false;

                //订单收款状态
                //假如已付清
                if ((model.Status == (int) Status.Payoff) || moneyReceivedStatus == MoneyReceivedStatus.Payoff)
                {
                    this.btnRemitAndPay.Visible = false;
                    this.btnAddPaymentorder.Visible = false;
                }

                //假如在等待汇款
                if (moneyReceivedStatus == MoneyReceivedStatus.WaitBuyerPay)
                {
                    this.btnRefund.Visible = false;
                }

                //订单送货状态
                //假如已签收
                if (model.DeliverStatus == (int) DeliverStatus.SiginIn)
                {
                    this.btnSendGoods.Visible = false;
                    this.btnReceived.Visible = false;
                    this.btnRefund.Visible = false;
                }
                //假如已发货
                else if (model.DeliverStatus == (int) DeliverStatus.GoodsSending)
                {
                    this.btnSendGoods.Visible = false;
                    this.btnReturnGoods.Visible = false;
                }
                //假如在配送中 || 物流失败
                else
                {
                    this.btnReceived.Visible = false;
                    this.btnReturnGoods.Visible = false;
                }

                //If(  已付清 & 已签收) 才能结清
                if ((model.Status != (int) Status.Payoff) || (model.DeliverStatus != (int) DeliverStatus.SiginIn))
                    this.btnEndOrder.Visible = false;

                if (model.MoneyReceived > 0)
                {
                    this.btnDeleteOrder.Visible = false;
                }


                //if已开发票
                if (model.Invoiced)
                    this.btnInvoice.OnClientClick = "return confirm('该订单已开好发票，确定还要开发票吗？')";
            }
            this.multiViewOrder.SetActiveView(this.viewShowOrderItem);
        }


        /// <summary>
        ///     订单详细记录绑定
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gridShowOrderItem_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                var lblPhotoId = (Label) e.Row.FindControl("lblPhotoId");
                var Photo = new GoodsManage();
                var photoModel = Photo.GetInfo(MyConvert.ToInt32(lblPhotoId.Text));

                if (photoModel != null)
                {
                    var lnkPhotoName = (HyperLink) e.Row.FindControl("lnkPhotoName");
                    lnkPhotoName.Text = photoModel.ContentTitle;
                    lnkPhotoName.NavigateUrl = photoModel.Link;

                    var lblUnit = (Label) e.Row.FindControl("lblUnit");
                    lblUnit.Text = photoModel.Unit;
                    var lblRemark = (Label) e.Row.FindControl("lblRemark");
                    lblRemark.Text = HttpUtility.HtmlDecode(lblRemark.Text);
                }
            }
        }


        //转向修改订单视图
        protected void btnModifyOrderInfo_Click(object sender, EventArgs e)
        {
            ViewModifyOrderDataBind(MyConvert.ToInt32(this.lblOrderId.Text));
            this.multiViewOrder.SetActiveView(this.viewModifyOrderInfo);
        }

        //转向确认订单视图
        protected void btnConfirmOrder_Click(object sender, EventArgs e)
        {
            var orderId = MyConvert.ToInt32(this.lblOrderId.Text);
            if (new OrdersManage().Exists(orderId))
            {
                new OrdersManage().UpdateOrderStatus(orderId, Status.Confimed);
            }
            ViewShowOrderItemDataBind(orderId);
        }

        //转向取消确定视图
        protected void btnCancelConfirmOrder_Click(object sender, EventArgs e)
        {
            var orderId = MyConvert.ToInt32(this.lblOrderId.Text);
            if (new OrdersManage().Exists(orderId))
            {
                new OrdersManage().UpdateOrderStatus(orderId, Status.WaitForConfirm);
            }
            ViewShowOrderItemDataBind(orderId);
        }

        //转向删除订单视图
        protected void btnDeleteOrder_Click(object sender, EventArgs e)
        {
            new OrdersManage().Delete(this.lblOrderId.Text.Trim());
            SwitchOrderListDataBindType();
        }

        //转向银行汇款支付视图
        protected void btnRemitAndPay_Click(object sender, EventArgs e)
        {
            ViewRemitAndPayDataBind(MyConvert.ToInt32(this.lblOrderId.Text, -1));
        }

        //转向虚拟货币支付视图
        protected void btnAddPaymentorder_Click(object sender, EventArgs e)
        {
            ViewAddPaymentDataBind(MyConvert.ToInt32(this.lblOrderId.Text, -1));
        }

        //转向退款视图
        protected void btnRefund_Click(object sender, EventArgs e)
        {
            ViewRefundDataBind(MyConvert.ToInt32(this.lblOrderId.Text, -1));
        }

        //转向录入发货信息视图
        protected void btnSendGoods_Click(object sender, EventArgs e)
        {
            ViewSaveDeliverInfoDataBind(MyConvert.ToInt32(this.lblOrderId.Text));
        }

        //客户已签收
        protected void btnReceived_Click(object sender, EventArgs e)
        {
            //更改订单送货状态为客户已签收

            var orderId = MyConvert.ToInt32(this.lblOrderId.Text);
            var order = new OrdersManage();
            if (order.Exists(orderId))
            {
                order.UpdateOrderDeliverStatus(orderId, DeliverStatus.SiginIn);
            }
            ViewShowOrderItemDataBind(orderId);
        }

        //转向退货信息视图
        protected void btnReturnGoods_Click(object sender, EventArgs e)
        {
            ViewReturnGoodsDataBind(MyConvert.ToInt32(this.lblOrderId.Text));
            this.multiViewOrder.SetActiveView(this.viewReturnGoods);
        }

        //开发票
        protected void btnInvoice_Click(object sender, EventArgs e)
        {
            ViewInvoiceDataBind(MyConvert.ToInt32(this.lblOrderId.Text));
        }

        //结清订单
        protected void btnEndOrder_Click(object sender, EventArgs e)
        {
            //更改订单状态为已结清,并赠送会员积分
            var order = new OrdersManage();
            var orderId = MyConvert.ToInt32(this.lblOrderId.Text);

            var model = order.GetInfo(MyConvert.ToInt32(this.lblOrderId.Text));
            if (order.Exists(orderId))
            {
                order.UpdateOrderStatus(orderId, Status.Finished);

                ViewShowOrderItemDataBind(model.Id);
            }
        }

        //转向打印视图
        protected void btnPrintOrder_Click(object sender, EventArgs e)
        {
            //panelNav.Visible = false;
            //panelSearch.Visible = false;
            ViewPrintOrderDataBind(MyConvert.ToInt32(this.lblOrderId.Text));
            this.multiViewOrder.SetActiveView(this.viewPrintOrder);
        }

        #endregion

        #region viewModifyOrderInfo

        //修改订单视图数据绑定
        protected void ViewModifyOrderDataBind(int orderId)
        {
            var order = new OrdersManage();
            var model = order.GetInfo(orderId);
            if (model == null)
                return;
            this.lblOrderId8.Text = model.Id.ToString();
            this.lblOrderNum8.Text = model.OrderNum;


            this.lblInputTime8.Text = model.CheckInDate.ToString();
            this.lblUserName8.Text = model.UserName;
            this.lblContacterName8.Text = model.ContacterName;
            this.lblContacterName8.Text = model.ContacterName;
            this.lblInvoiced8.Text = model.Invoiced ? "√" : "×";
            this.lblCheckOutStatus8.Text = order.GetMoneyReceivedStatusDescription(model.Id);
            this.lblDeliverStatus8.Text = order.GetDeliverStatusDescription(model.Id);
            this.lblStatus8.Text = order.GetOrderStatusDescription(model.Id);
            this.lblNeedInvoice8.Text = model.Invoiced ? "√" : "×";

            this.txtContacterName8.Text = model.ContacterName;
            this.txtAddress8.Text = model.Address;
            this.txtZipCode8.Text = model.ZipCode;
            this.txtPhone8.Text = model.ZipCode;
            this.txtEmail8.Text = model.Email;
            this.txtMobile8.Text = model.Mobile;


            var paymentType = new PaymentMethodManage();
            var paymentTypeDataset = paymentType.GetList();
            this.drpPaymentType8.Items.Clear();

            if (paymentTypeDataset.Count > 0)
            {
                foreach (var pm in paymentTypeDataset)
                {
                    if (pm.Pass)
                    {
                        var li = new ListItem {Text = pm.MethodName, Value = pm.Id.ToString()};


                        this.drpPaymentType8.Items.Add(li);
                    }
                }
                this.drpPaymentType8.SelectedValue = model.PaymentType.ToString();
            }


            var deliverType = new DeliveryMethodManage();
            var deliverTypeDataset = deliverType.GetList();
            this.drpDeliverType8.Items.Clear();

            if (deliverTypeDataset.Count > 0)
            {
                foreach (var pm in deliverTypeDataset)
                {
                    if (pm.Pass == 1)
                    {
                        var li = new ListItem {Text = pm.MethodName, Value = pm.Id.ToString()};
                        li.Attributes["title"] = Server.HtmlEncode(pm.Intro);

                        this.drpDeliverType8.Items.Add(li);
                    }
                }
                this.drpDeliverType8.SelectedValue = model.DeliverType.ToString();
            }


            if (model.Invoiced)
                this.chkNeedInvoice8.Checked = true;
            else
                this.chkNeedInvoice8.Checked = false;
            this.txtChargeDeliver8.Text = model.ChargeDeliver.ToString("N2");
            this.txtInvoiceContent8.Text = HttpUtility.HtmlDecode(model.InvoiceContent).Replace("<br>", "\r\n");
            this.txtRemark8.Text = HttpUtility.HtmlDecode(model.Remark).Replace("<br>", "\r\n");
            ;

            //if订单已经确认,将订单商品数据锁定

            this.drpDeliverType8.Enabled = model.Status == (int) Status.WaitForConfirm;
            this.drpPaymentType8.Enabled = model.Status == (int) Status.WaitForConfirm;
            this.txtChargeDeliver8.ReadOnly = model.Status != (int) Status.WaitForConfirm;
            this.chkNeedInvoice8.Enabled = model.Status == (int) Status.WaitForConfirm;


            var orderItem = new OrderListManage();

            this.gridModifyOrderItems.DataSource = orderItem.GetList(model.Id);
            this.gridModifyOrderItems.DataBind();
        }


        // 商品详细记录数据绑定
        protected void gridModifyOrderItems_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                var order = new OrdersManage();
                var orderModel = order.GetInfo(MyConvert.ToInt32(this.lblOrderId8.Text));
                if (orderModel == null)
                    return;

                var txtQuantity8 = (TextBox) e.Row.FindControl("txtQuantity8");
                var txtPrice8 = (TextBox) e.Row.FindControl("txtPrice8");

                txtQuantity8.ReadOnly = orderModel.Status != (int) Status.WaitForConfirm ? true : false;
                txtPrice8.ReadOnly = orderModel.Status != (int) Status.WaitForConfirm ? true : false;

                var lblPhotoId = (Label) e.Row.FindControl("lblPhotoId8");
                var Photo = new GoodsManage();
                var photoModel = Photo.GetInfo(MyConvert.ToInt32(lblPhotoId.Text));

                if (photoModel != null)
                {
                    var txtPhotoName = (Label) e.Row.FindControl("txtPhotoName8");
                    txtPhotoName.Text = photoModel.ContentTitle;

                    var txtUnit = (Label) e.Row.FindControl("txtUnit8");
                    txtUnit.Text = photoModel.Unit;
                }
            }
        }

        //根据选择送货方式自动填充运费项目
        protected void listDeliverType8_SelectedIndexChanged(object sender, EventArgs e)
        {
            var deliver = new DeliveryMethodManage();
            var deliverType = MyConvert.ToInt32(this.drpDeliverType8.SelectedValue);
            var deliverTypeModel = deliver.GetInfo(deliverType);
            this.txtChargeDeliver8.Text = deliverTypeModel.Charge.ToString();
        }

        //取消修改订单
        protected void btnCancelModifyOrder_Click(object sender, EventArgs e)
        {
            ViewShowOrderItemDataBind(MyConvert.ToInt32(this.lblOrderId8.Text));
            this.multiViewOrder.SetActiveView(this.viewShowOrderItem);
        }

        // 修改订单(orders表)
        protected void btnConfirmModifyOrder_Click(object sender, EventArgs e)
        {
            var needInvoice = this.chkNeedInvoice8.Checked;

            var remark = (this.txtRemark8.Text.Trim());
            var contacterName = (this.txtContacterName8.Text.Trim());
            var address = (this.txtAddress8.Text.Trim());
            var zipCode = (this.txtZipCode8.Text.Trim());
            var mobile = (this.txtMobile8.Text.Trim());
            var phone = (this.txtPhone8.Text.Trim());
            var email = (this.txtEmail8.Text.Trim());


            var paymentType = MyConvert.ToInt32(this.drpPaymentType8.Text);
            var deliverType = MyConvert.ToInt32(this.drpDeliverType8.Text);
            var chargeDeliver = decimal.Parse(this.txtChargeDeliver8.Text.Trim());

            var invoiceContent = (this.txtInvoiceContent8.Text.Trim());

            //将订单写入数据库
            var order = new OrdersManage();
            var orderModel = order.GetInfo(MyConvert.ToInt32(this.lblOrderId8.Text));
            if (orderModel == null)
                return;
            orderModel.ContacterName = HttpUtility.HtmlEncode(contacterName);
            orderModel.Address = HttpUtility.HtmlEncode(address);
            orderModel.Email = HttpUtility.HtmlEncode(email);
            orderModel.PaymentType = paymentType;
            orderModel.NeedInvoice = needInvoice;
            orderModel.InvoiceContent = HttpUtility.HtmlEncode(invoiceContent);
            orderModel.Phone = HttpUtility.HtmlEncode(phone);
            orderModel.ZipCode = HttpUtility.HtmlEncode(zipCode);
            orderModel.Mobile = HttpUtility.HtmlEncode(mobile);
            orderModel.DeliverType = deliverType;
            orderModel.ChargeDeliver = chargeDeliver;
            orderModel.Remark = HttpUtility.HtmlEncode(remark);
            orderModel.MoneyTotal = orderModel.MoneyGoods + chargeDeliver;

            var resu = order.Add(orderModel, null); //修改订单

            if (string.IsNullOrEmpty(resu.Errors))
            {
                ViewModifyOrderDataBind(MyConvert.ToInt32(this.lblOrderId.Text));
                MsgBox.Message("修改完成!", this.UpdatePanel1);
            }
            else
            {
                MsgBox.Message(resu.Errors, this.UpdatePanel1);
            }
        }


        //修改订单详细记录（orderItems表)
        protected void gridModifyOrderItems_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
            //修改orderItems表中对应的记录的信息
            var index = e.NewSelectedIndex;
            Decimal truePrice = 0;
            var Quantity = 0;
            var txtQuantity8 = (TextBox) this.gridModifyOrderItems.Rows[index].FindControl("txtQuantity8");
            var txtPrice = (TextBox) this.gridModifyOrderItems.Rows[index].FindControl("txtPrice8");
            var txtRemark = (TextBox) this.gridModifyOrderItems.Rows[index].FindControl("txtRemark8");
            var lblItemId = (Label) this.gridModifyOrderItems.Rows[index].FindControl("lblItemId8");

            //提示输入错误信息
            if (!Decimal.TryParse(txtPrice.Text, out truePrice))
            {
                MsgBox.Message("指定价输入错误，请重新输入！", this.UpdatePanel1);
                ;
                return;
            }

            if (!int.TryParse(txtQuantity8.Text, out Quantity))
            {
                MsgBox.Message("商品数量输入错误，请重新输入！", this.UpdatePanel1);
                ;
                return;
            }

            var orderItem = new OrderListManage();
            var orderItemModel = orderItem.GetInfo(MyConvert.ToInt32(lblItemId.Text.Trim()));
            if (orderItemModel != null)
            {
                orderItemModel.Price = truePrice;
                orderItemModel.SubTotal = truePrice*Quantity;
                orderItemModel.Remark = (txtRemark.Text.Trim());

                string Err = null;
                orderItem.Edit(orderItemModel, ref Err);

                if (string.IsNullOrEmpty(Err) == false)
                {
                    MsgBox.Message(Err, this.UpdatePanel1);
                    return;
                }
                MsgBox.Message("已修改.", this.UpdatePanel1);
            }


            //重新绑定数据
            ViewModifyOrderDataBind(MyConvert.ToInt32(this.lblOrderId8.Text));
        }

        #endregion

        #region viewRemitAndPay

        //填充视图数据
        protected void ViewRemitAndPayDataBind(int orderId)
        {
            var order = new OrdersManage();
            var model = order.GetInfo(orderId);
            string msg;
            if (model != null)
            {
                if (order.IsPaid(model.Id))
                {
                    msg = " 该订单已付清！";
                    MsgBox.Message(msg, this.UpdatePanel1);

                    ViewShowOrderItemDataBind(orderId);
                    this.multiViewOrder.SetActiveView(this.viewShowOrderItem);
                    return;
                }
                this.lblOrderId9.Text = orderId.ToString();
                this.lblOrderNum9.Text = model.OrderNum;
                this.lblMoneyTotal9.Text = string.Format("{0:0}", model.MoneyTotal);
                this.lblUserName9.Text = model.UserName;
                this.lblContacterName9.Text = model.ContacterName;
                this.lblMoneyReceived9.Text = string.Format("{0:0}", model.MoneyReceived);
                this.txtMoney9.Text = Convert.ToString((model.MoneyTotal - model.MoneyReceived));
                this.lblMoneyMsg9.Text = string.Empty;
                this.txtDateAndTime9.Text = DateTime.Now.ToString("yyyy-MM-dd");
                drpBank9_DataBind();
                this.multiViewOrder.SetActiveView(this.viewRemitAndPay);
            }
            else
            {
                msg = "该订单不存在！";
                MsgBox.Message(msg, this.UpdatePanel1);

                ViewShowOrderItemDataBind(orderId);
                this.multiViewOrder.SetActiveView(this.viewShowOrderItem);
            }
        }

        private void drpBank9_DataBind()
        {
            var bank = new BankManage();
            this.drpBank9.DataSource = bank.GetList();
            this.drpBank9.DataTextField = "BankName";
            this.drpBank9.DataValueField = "ID";
            this.drpBank9.DataBind();
        }


        //保存汇款信息
        protected void btnSaveRemitAndPayInfo_Click(object sender, EventArgs e)
        {
            //判断输入信息有没有出错

            Decimal money = 0;
            var isErr = false;
            var order = new OrdersManage();
            var orderModel = order.GetInfo(MyConvert.ToInt32(this.lblOrderId9.Text));
            if (orderModel != null)
            {
                if (order.IsPaid(orderModel.Id))
                {
                    ViewShowOrderItemDataBind(orderModel.Id);
                    this.multiViewOrder.SetActiveView(this.viewShowOrderItem);
                    return;
                }
            }
            var dateAndTime = DateTime.Now;
            if (!DateTime.TryParse(this.txtDateAndTime9.Text.Trim(), out dateAndTime))
            {
                isErr = true;
            }
            if (!decimal.TryParse(this.txtMoney9.Text.Trim(), out money))
            {
                isErr = true;
                this.lblMoneyMsg9.Text = "汇款金额输入错误！";
            }
            if (money == 0)
            {
                isErr = true;
                this.lblMoneyMsg9.Text = "汇款金额不能为０！";
            }
            if (money > (orderModel.MoneyTotal - orderModel.MoneyReceived))
            {
                isErr = true;
                this.lblMoneyMsg9.Text = "汇款金额不能大于所欠货款！";
            }
            if (isErr)
            {
                return;
            }
            if (orderModel != null)
            {
                //保存信息到bankrollItem表（先汇入）
                var bankroolItem = new BankrollListManage();
                var model = new BankrollList();
                model.OrderId = orderModel.Id;
                model.UserName = this.lblUserName9.Text;
                //model.UserName = orderModel.UserName;
                model.CheckInDate = DateTime.Now;
                model.Money = money;
                model.MoneyType = (int) HowPay.Remit;
                model.CurrencyType = (int) CurrencyType.Rmb;
                model.EBankId = int.Parse(this.drpBank9.SelectedValue);
                model.Bank = this.drpBank9.Text;
                model.IncomePayOut = (int) IncomePayout.Income;
                model.Id = 0;
                model.PaymentId = 0;
                model.Remark = "用户汇款";
                model.LogTime = DateTime.Now;
                model.Admin = UserManage.Current.UserName;
                model.IP = HttpClient.GetIP();

                string Err = null;
                bankroolItem.Add(model, ref Err);

                if (string.IsNullOrEmpty(Err) == false)
                {
                    MsgBox.Message(Err, this.UpdatePanel1);
                    return;
                }
                //保存信息到bankrollItem表（再扣除）
                model.Money = -money;
                model.MoneyType = (int) HowPay.PayByBalance;
                model.EBankId = 0;
                model.Bank = string.Empty;
                model.IncomePayOut = (int) IncomePayout.Payout;
                model.Id = MyConvert.ToInt32(this.lblOrderId9.Text);
                model.Remark = "支付订单费用，订单号：" + orderModel.OrderNum + "  " + this.txtRemark9.Text;


                var msg = string.Empty;

                bankroolItem.Add(model, ref Err);

                if (string.IsNullOrEmpty(Err) == false)
                {
                    MsgBox.Message("系统错误发生错误，请重新汇款！ ", this.UpdatePanel1);
                    return;
                }

                //更新订单信息

                orderModel.MoneyReceived = money + orderModel.MoneyReceived;
                if (orderModel.MoneyReceived > 0)
                {
                    orderModel.Status = (int) Status.ReceivedEarnest;
                    if (orderModel.MoneyReceived == orderModel.MoneyTotal)
                        orderModel.Status = (int) Status.Payoff;
                }
                var resu = order.Add(orderModel, null);

                if (string.IsNullOrEmpty(Err) == false)
                {
                    MsgBox.Message(Err, this.UpdatePanel1);
                    return;
                }
                //提示成功信息

                msg = "操作成功！";
                MsgBox.Message(msg, this.UpdatePanel1);
                ViewShowOrderItemDataBind(orderModel.Id);
                this.multiViewOrder.SetActiveView(this.viewShowOrderItem);
            }
        }

        #endregion

        #region viewRefund

        protected void ViewRefundDataBind(int orderId)
        {
            var order = new OrdersManage();
            var model = order.GetInfo(orderId);
            string msg;
            if (model != null)
            {
                if (model.MoneyReceived <= 0)
                {
                    msg = "该订单所付货款为0！";

                    MsgBox.Message(msg, this.UpdatePanel1);

                    ViewShowOrderItemDataBind(orderId);
                    this.multiViewOrder.SetActiveView(this.viewShowOrderItem);
                    return;
                }


                this.lblOrderId7.Text = orderId.ToString();
                this.lblOrderNum7.Text = model.OrderNum;
                this.lblMoneyTotal7.Text = string.Format("{0:0}", model.MoneyTotal);
                this.lblUserName7.Text = model.UserName;
                this.lblContacterName7.Text = model.ContacterName;
                this.lblMoneyReceived7.Text = string.Format("{0:0}", model.MoneyReceived);
                this.txtMoney7.Text = Convert.ToString(model.MoneyReceived);
                this.lblMoneyMsg7.Text = "<span style=\"color: #0000ff\">退款金额将从已付款中扣除。</span>";
                this.txtDateAndTime7.Text = DateTime.Now.ToString();
                this.txtRemark7.Text = " 订单退款金额，订单号：" + model.OrderNum;

                this.multiViewOrder.SetActiveView(this.viewRefund);
            }
            else
            {
                msg = "该订单不存在！";
                MsgBox.Message(msg, this.UpdatePanel1);
                ViewShowOrderItemDataBind(orderId);
                this.multiViewOrder.SetActiveView(this.viewShowOrderItem);
            }
        }

        //保存退款信息
        protected void btnRefund7_Click(object sender, EventArgs e)
        {
            var order = new OrdersManage();

            var orderModel = order.GetInfo(MyConvert.ToInt32(this.lblOrderId7.Text));

            var msg = string.Empty;

            if (orderModel != null)
            {
                if (orderModel.MoneyReceived <= 0)
                {
                    msg = "该订单所付货款为0！";
                    MsgBox.Message(msg, this.UpdatePanel1);

                    ViewShowOrderItemDataBind(orderModel.Id);
                    this.multiViewOrder.SetActiveView(this.viewShowOrderItem);
                    return;
                }
            }

            else
            {
                msg = "该订单不存在！";
                MsgBox.Message(msg, this.UpdatePanel1);

                ViewShowOrderItemDataBind(orderModel.Id);
                this.multiViewOrder.SetActiveView(this.viewShowOrderItem);
                return;
            }


            //判断输入信息有没有出错
            var dateAndTime = DateTime.Now;
            Decimal money = 0;
            var isErr = false;
            if (!DateTime.TryParse(this.txtDateAndTime7.Text.Trim(), out dateAndTime))
            {
                isErr = true;
            }
            if (!decimal.TryParse(this.txtMoney7.Text.Trim(), out money))
            {
                isErr = true;
                this.lblMoneyMsg7.Text = "退款金额输入错误！";
            }
            if (money > orderModel.MoneyReceived)
            {
                isErr = true;
                this.lblMoneyMsg7.Text = "退款金额不能大于订单已付款！";
            }

            if (money <= 0)
            {
                isErr = true;
                this.lblMoneyMsg7.Text = "退款金额须为正数！";
            }
            if (isErr)
            {
                return;
            }
            if (orderModel != null)
            {
                //保存信息到bankrollItem表
                var bankroolItem = new BankrollListManage();
                var model = new BankrollList();
                model.UserName = this.lblUserName7.Text;
                //model.ClientId = orderModel.ClientId;
                model.CheckInDate = dateAndTime;
                if (this.radRefundType.SelectedValue == "0")
                    model.Money = money;
                else
                    model.Money = 0;
                model.MoneyType = (int) HowPay.PayByBalance;
                model.CurrencyType = (int) CurrencyType.Rmb;
                model.EBankId = 0;
                model.Bank = string.Empty;
                model.IncomePayOut = (int) IncomePayout.Income;
                model.Id = MyConvert.ToInt32(this.lblOrderId7.Text);
                model.PaymentId = 0;
                model.Remark = this.txtRemark7.Text;
                model.LogTime = DateTime.Now;
                model.Admin = UserManage.Current.UserName;
                model.IP = HttpClient.GetIP();

                string Err = null;
                bankroolItem.Add(model, ref Err);

                if (string.IsNullOrEmpty(Err) == false)
                {
                    MsgBox.Message("系统错误发生错误，请重新汇款！ ", this.UpdatePanel1);
                    return;
                }
                //更新订单信息
                //将订单状态结清改为已经确认

                orderModel.MoneyReceived = orderModel.MoneyReceived - money;

                if (orderModel.MoneyReceived <= 0)
                    orderModel.Status = (int) Status.Confimed;
                else
                    orderModel.Status = (int) Status.ReceivedEarnest;

                var resu = order.Add(orderModel, null);

                if (string.IsNullOrEmpty(resu.Errors) == false)
                {
                    MsgBox.Message(resu.Errors, this.UpdatePanel1);
                    return;
                }

                //if选择扣除的金额添加到会员资金余额中
                if (this.radRefundType.SelectedValue == "0")
                {
                    //WinnerCMS.BLL.MemberManage user = new User();
                    //MemberInfo userModel = new MemberInfo();
                    //userModel = user.GetInfo(model.UserName);
                    //if (userModel != null)
                    //{
                    //    userModel.Balance = userModel.Balance + money;
                    //    user.Update(userModel);
                    //}
                }

                //提示成功信息

                MsgBox.Message("操作成功！", this.UpdatePanel1);

                ViewShowOrderItemDataBind(orderModel.Id);
                this.multiViewOrder.SetActiveView(this.viewShowOrderItem);
            }
        }

        #endregion

        #region viewSaveDeliverInfo

        /// <summary>
        ///     绑定录入信息视图数据
        /// </summary>
        /// <param name="orderId"></param>
        protected void ViewSaveDeliverInfoDataBind(int orderId)
        {
            var order = new OrdersManage();
            var orderModel = order.GetInfo(orderId);
            if (orderModel != null)
            {
                var user = new UserManage();

                if (user.GetInfo(orderModel.UserName) != null)
                {
                    this.lblRealName5.Text = user.GetInfo(orderModel.UserName).TrueName;
                }
                this.lblOrderId5.Text = orderId.ToString();
                this.lblUserName5.Text = orderModel.UserName;
                this.lblContacterName5.Text = orderModel.ContacterName;
                this.lblOrderNum5.Text = orderModel.OrderNum;
                this.lblMoneyTotal5.Text = string.Format("{0:c}", orderModel.MoneyTotal);
                this.lblMoneyReceived5.Text = string.Format("{0:c}", orderModel.MoneyReceived);
                this.lblHandlerNameMsg5.Text = string.Empty;
                this.lblDeliverDateMsg5.Text = string.Empty;

                var deliverType = new DeliveryMethodManage();
                var deliverTypeModel = deliverType.GetInfo(orderModel.DeliverType);
                if (deliverTypeModel != null)
                {
                    this.lblDeliverTypeName5.Text = deliverTypeModel.MethodName;
                }

                this.txtDeliverDate5.Text = DateTime.Now.Date.ToString("yyyy-MM-dd");

                this.multiViewOrder.SetActiveView(this.viewSaveDeliverInfo);
            }
        }

        //录入发货信息到deliverItem表
        protected void btnDeliverConfirm_Click(object sender, EventArgs e)
        {
            //检查输入信息

            var deliverDate = DateTime.Now;
            var expressCompany = (this.txtExpressCompany5.Text.Trim());
            var expressNum = (this.txtExpressNum5.Text.Trim());
            var handlerName = (this.txtHandlerName5.Text.Trim());
            var remark = (this.txtRemark5.Text.Trim());

            var isErr = false;

            if (string.IsNullOrEmpty(handlerName))
            {
                isErr = true;
                this.lblHandlerNameMsg5.Text = "请输入经手人！";
            }
            if (isErr)
                return;

            //录入发货信息到deliverItem表

            var model = new Delivery();
            var deliverItem = new DeliveryManage();

            model.OrderId = MyConvert.ToInt32(this.lblOrderId5.Text);
            model.DeliverDate = deliverDate;
            model.DeliverDirection = (int) DeliverDirection.SendGoods;
            model.HandlerName = handlerName;
            model.ExpressCompany = expressCompany;
            model.ExpressNumber = expressNum;
            model.Admin = UserManage.Current.UserID;
            model.Remark = remark;
            model.Served = 0;

            var resu = deliverItem.Add(model);

            if (string.IsNullOrEmpty(resu.Errors))
            {
                //更新订单信息
                var order = new OrdersManage();
                var orderModel = order.GetInfo(model.OrderId);
                orderModel.DeliverStatus = (int) DeliverStatus.GoodsSending;

                order.Add(orderModel, null);

                if (string.IsNullOrEmpty(resu.Errors) == false)
                {
                    MsgBox.Message(resu.Errors, this.UpdatePanel1);
                    return;
                }


                //更新Photo表库存量
                var orderItem = new OrderListManage();
                var orderItemDataset = orderItem.GetList(orderModel.Id);
                var photo = new GoodsManage();

                foreach (var dr in orderItemDataset)
                {
                    var orderItemModel = orderItem.GetInfo(dr.Id);
                    var photoModel = photo.GetInfo(orderItemModel.GoodsId);
                    photoModel.Stocks = photoModel.Stocks - orderItemModel.Quantity;
                    photoModel.BuyTimes = photoModel.BuyTimes + orderItemModel.Quantity;

                    var p = photo.Add(photoModel, "", new List<string>(), new List<object>());

                    if (string.IsNullOrEmpty(p.Errors) == false)
                    {
                        MsgBox.Message(p.Errors, this.UpdatePanel1);
                        return;
                    }
                }


                //提示成功信息

                MsgBox.Message("操作成功！", this.UpdatePanel1);

                ViewShowOrderItemDataBind(model.OrderId);
                this.multiViewOrder.SetActiveView(this.viewShowOrderItem);
            }
            else
            {
                MsgBox.Message("系统错误发生错误，请重新发货！", this.UpdatePanel1);
            }
        }

        #endregion

        #region viewReturnGoods

        protected void ViewReturnGoodsDataBind(int orderId)
        {
            var order = new OrdersManage();
            var orderModel = order.GetInfo(orderId);
            if (orderModel != null)
            {
                var user = new UserManage();
                if (user.GetInfo(orderModel.UserName) != null)
                {
                    this.lblRealName4.Text = user.GetInfo(orderModel.UserName).TrueName;
                }
                this.lblOrderId4.Text = orderId.ToString();
                this.lblUserName4.Text = orderModel.UserName;
                this.lblContacterName4.Text = orderModel.ContacterName;
                this.lblOrderNum4.Text = orderModel.OrderNum;
                this.lblMoneyTotal4.Text = string.Format("{0:c}", orderModel.MoneyTotal);
                this.lblMoneyReceived4.Text = string.Format("{0:c}", orderModel.MoneyReceived);
                this.lblHandlerNameMsg4.Text = string.Empty;
                var deliverType = new DeliveryMethodManage();
                var deliverTypeModel = deliverType.GetInfo(orderModel.DeliverType);
                if (deliverTypeModel != null)
                {
                    this.lblDeliverTypeName4.Text = deliverTypeModel.MethodName;
                }

                this.txtDeliverDate4.Text = DateTime.Now.Date.ToString("yyyy-MM-dd");
            }
        }

        protected void btnReturnGoodsConfirm4_Click(object sender, EventArgs e)
        {
            //检查输入信息

            DateTime returnGoodsDate;
            var expressCompany = string.Empty;
            var expressNum = string.Empty;
            var handlerName = (this.txtHandlerName4.Text.Trim());
            var remark = (this.txtRemark4.Text.Trim());

            var isErr = false;
            if (!DateTime.TryParse(this.txtDeliverDate4.Text.Trim(), out returnGoodsDate))
            {
                isErr = true;
                this.lblDeliverDateMsg4.Text = "发货时间格式错误！";
            }
            if (string.IsNullOrEmpty(handlerName))
            {
                isErr = true;
                this.lblHandlerNameMsg4.Text = "请输入经手人！";
            }
            if (isErr)
                return;

            //录入退货信息到deliverItem表

            var model = new Delivery();
            var deliverItem = new DeliveryManage();

            model.OrderId = MyConvert.ToInt32(this.lblOrderId4.Text);
            model.DeliverDate = returnGoodsDate;
            model.DeliverDirection = (int) DeliverDirection.ReturnGoods;
            model.HandlerName = handlerName;
            model.ExpressCompany = expressCompany;
            model.ExpressNumber = expressNum;
            model.Admin = UserManage.Current.UserID;
            model.Remark = remark;


            var order = new OrdersManage();
            var orderModel = order.GetInfo(model.OrderId);

            if (orderModel != null)
                model.Served = orderModel.DeliverStatus;


            var resu = deliverItem.Add(model);

            if (string.IsNullOrEmpty(resu.Errors))
            {
                //更新订单信息
                orderModel.DeliverStatus = (int) DeliverStatus.Preparative;

                var o = order.Add(orderModel, null);
                if (string.IsNullOrEmpty(o.Errors) == false)
                {
                    MsgBox.Message(o.Errors, this.UpdatePanel1);
                    return;
                }


                //更新Photo表库存量和订单数量
                var orderItem = new OrderListManage();
                var orderItemDataset = orderItem.GetList(orderModel.Id);
                var photo = new GoodsManage();


                foreach (var dr in orderItemDataset)
                {
                    var orderItemModel = orderItem.GetInfo(dr.Id);
                    var photoModel = photo.GetInfo(orderItemModel.GoodsId);
                    //PhotoModel.GoodsNum = PhotoModel.GoodsNum + orderItemModel.Quantity;
                    photoModel.Stocks = photoModel.Stocks + orderItemModel.Quantity;
                    photoModel.BuyTimes = photoModel.BuyTimes - orderItemModel.Quantity;

                    photo.Add(photoModel, "", new List<string>(), new List<object>());
                }


                //提示成功信息

                MsgBox.Message("操作成功！", this.UpdatePanel1);

                ViewShowOrderItemDataBind(model.OrderId);
                this.multiViewOrder.SetActiveView(this.viewShowOrderItem);
            }
            else
            {
                MsgBox.Message("系统错误发生错误，请重新退货！", this.UpdatePanel1);
            }
        }

        #endregion

        #region viewPrintOrder

        /// <summary>
        ///     打印订单视图数据绑定
        /// </summary>
        /// <param name="orderId"></param>
        protected void ViewPrintOrderDataBind(int orderId)
        {
            var order = new OrdersManage();
            var model = order.GetInfo(orderId);
            this.lblOrderId6.Text = model.Id.ToString();
            this.lblOrderNum6.Text = model.OrderNum;
            this.lblInputTime6.Text = model.CheckInDate.ToString();
            this.lblUserName6.Text = model.UserName;
            this.lblContacterName6.Text = model.ContacterName;
            this.lblContacterName62.Text = model.ContacterName;
            this.lblAddress6.Text = model.Address;
            this.lblZipCode6.Text = model.ZipCode;
            this.lblPhone6.Text = model.ZipCode;
            this.lblEmail6.Text = model.Email;
            this.lblMobile6.Text = model.Mobile;
            this.lblInvoiced6.Text = model.Invoiced ? "√" : "×";


            this.lblCheckOutStatus6.Text = order.GetMoneyReceivedStatusDescription(model.Id);

            this.lblDeliverStatus6.Text = order.GetDeliverStatusDescription(model.Id);

            this.lblStatus6.Text = order.GetOrderStatusDescription(model.Id);


            var paymentType = new PaymentMethodManage();
            var paymentTypeModel = paymentType.GetInfo(model.PaymentType);
            if (paymentTypeModel != null) this.lblPaymentType6.Text = paymentTypeModel.MethodName;

            var deliverType = new DeliveryMethodManage();
            var deliverTypeModel = deliverType.GetInfo(model.DeliverType);
            if (deliverTypeModel != null) this.lblDeliverType6.Text = deliverTypeModel.MethodName;


            this.lblPresentExp6.Text = model.PresentExp.ToString();
            this.lblMoneyGoods6.Text = string.Format("{0:c}", model.MoneyGoods);
            this.lblChargeDeliver6.Text = string.Format("{0:c}", model.ChargeDeliver);

            if (model.Invoiced)
                this.lblNeedInvoice6.Text = "√";
            else
                this.lblNeedInvoice6.Text = "×";
            this.lblInvoiceContent6.Text = HttpUtility.HtmlDecode(model.InvoiceContent).Replace("\r\n", "<br>");
            this.lblRemark6.Text = HttpUtility.HtmlDecode(model.Remark).Replace("\r\n", "<br>");


            this.lblMoneyGoods62.Text = string.Format("{0:c}", model.MoneyGoods);
            this.lblChargeDeliver62.Text = string.Format("{0:c}", model.ChargeDeliver);
            this.lblChargeDeliver63.Text = string.Format("{0:c}", model.ChargeDeliver);
            this.lblTotal6.Text = string.Format("{0:c}", model.MoneyTotal);
            this.lblTotal62.Text = string.Format("{0:c}", model.MoneyTotal);
            this.lblMoneyReceived6.Text = string.Format("{0:c}", model.MoneyReceived);
            this.lblArrearage6.Text = string.Format("{0:c}", model.MoneyTotal - model.MoneyReceived);


            var orderItem = new OrderListManage();

            this.gridPrintOrder6.DataSource = orderItem.GetList(model.Id);
            this.gridPrintOrder6.DataBind();
        }

        protected void gridPrintOrder6_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                var lblPhotoId = (Label) e.Row.FindControl("lblPhotoId");
                var photo = new GoodsManage();
                var photoModel = photo.GetInfo(MyConvert.ToInt32(lblPhotoId.Text));

                if (photoModel != null)
                {
                    var lblPhotoName = (Label) e.Row.FindControl("lblPhotoName");
                    lblPhotoName.Text = photoModel.ContentTitle;
                    var lblUnit = (Label) e.Row.FindControl("lblUnit");
                    lblUnit.Text = photoModel.Unit;
                    var lblRemark = (Label) e.Row.FindControl("lblRemark");
                    lblRemark.Text = HttpUtility.HtmlDecode(lblRemark.Text);
                }
            }
        }


        //取消打印，查看订单　
        protected void btnToViewOrder6_Click(object sender, EventArgs e)
        {
            //panelSearch.Visible = true;
            //panelNav.Visible = true;
            ViewShowOrderItemDataBind(MyConvert.ToInt32(this.lblOrderId6.Text));
            this.multiViewOrder.SetActiveView(this.viewShowOrderItem);
        }

        #endregion

        #region viewAddPayment 暂时作废。

        /// <summary>
        ///     余额支付数据填充
        /// </summary>
        /// <param name="orderId"></param>
        /// <remarks>暂时作废。</remarks>
        public void ViewAddPaymentDataBind(int orderId)
        {
            //OrdersManage order = new OrdersManage();
            //Order model = new Order();
            //model = order.GetInfo(orderId);


            //MemberManage user = new MemberManage();
            //MemberInfo userModel = new MemberInfo();
            //userModel = user.GetInfo("");

            //string msg;
            //if (model != null && userModel != null)
            //{
            //    if (order.IsPaid(model.ID))
            //    {

            //        msg = "该订单已付清！";
            //        MsgBox.Alert(msg, UpdatePanel1);
            //        ViewShowOrderItemDataBind(orderId);
            //        multiViewOrder.SetActiveView(viewShowOrderItem);
            //        return;
            //    }
            //    if (userModel.Balance < (model.MoneyTotal - model.MoneyReceived))
            //    {
            //        msg = "您的资金余额不足！";
            //        MsgBox.Alert(msg, UpdatePanel1);

            //        ViewShowOrderItemDataBind(orderId);
            //        multiViewOrder.SetActiveView(viewShowOrderItem);
            //        return;

            //    }


            //    lblAddPayment_orderId.Text = orderId.ToString();
            //    lblAddPayment_balance.Text = userModel.Balance.ToString();

            //    lblAddPayment_moneyTotal.Text = model.MoneyTotal.ToString();
            //    lblAddPayment_orderNum.Text = model.OrderNum;
            //    lblAddPayment_moneyReceived.Text = model.MoneyReceived.ToString();
            //    lblAddPayment_userName.Text = model.UserName;
            //    txtAddPayment_payout.Text = Convert.ToString((model.MoneyTotal - model.MoneyReceived));
            //    lblAddPayment_payoutMsg.Text = "<span style=\"color:black\">支付成功后，将从您的资金余额中扣除相应款项。</span>";
            //    txtAddPayment_remark.Text = model.OrderNum;

            //    multiViewOrder.SetActiveView(viewAddPayment);
            //}
            //else
            //{
            //    msg = "该订单不存在！";
            //    MsgBox.Alert(msg, UpdatePanel1);

            //    ViewShowOrderItemDataBind(orderId);
            //    multiViewOrder.SetActiveView(viewShowOrderItem);
            //    return;
            //}
        }


        //虚拟货币支付
        protected void btnAddPayment_confirm_Click(object sender, EventArgs e)
        {
            ////判断输入信息有没有出错
            //DateTime dateAndTime = DateTime.Now;
            //Decimal money = 0;
            //bool isErr = false;
            //OrdersManage order = new OrdersManage();
            //Order orderModel = new Order();

            //WinnerCMS.BLL.MemberManage user = new WinnerCMS.BLL.MemberManage();
            //MemberInfo userModel = new MemberInfo();
            //userModel = user.GetInfo(AdminName);

            //orderModel = order.GetInfo(MyConvert.StrToInt(lblAddPayment_orderId.Text));
            //if (orderModel != null)
            //{
            //    if (orderModel.MoneyReceived >= orderModel.MoneyTotal)
            //    {
            //        ViewShowOrderItemDataBind(orderModel.ID);
            //        multiViewOrder.SetActiveView(viewShowOrderItem);
            //        return;
            //    }
            //}

            //if (!decimal.TryParse(txtAddPayment_payout.Text.Trim(), out money))
            //{
            //    isErr = true;
            //    lblAddPayment_payoutMsg.Text = "汇款金额输入错误！";
            //}
            //if (money == 0)
            //{
            //    isErr = true;
            //    lblAddPayment_payoutMsg.Text = "汇款金额不能为０！";
            //}
            //if (money > (orderModel.MoneyTotal - orderModel.MoneyReceived))
            //{
            //    isErr = true;
            //    lblAddPayment_payoutMsg.Text = "金额不能大于所欠货款！";
            //}

            //if (userModel.Balance < (orderModel.MoneyTotal - orderModel.MoneyReceived))
            //{
            //    isErr = true;
            //    lblAddPayment_payoutMsg.Text = "您的资金余额不足！";
            //}

            //if (isErr)
            //{
            //    return;
            //}

            //if (orderModel != null)
            //{
            //    //保存信息到bankrollItem表（扣除）
            //    BankrollListManage bankroolItem = new BankrollListManage();
            //    BankrollListInfo model = new BankrollListInfo();
            //    model.UserName = lblAddPayment_userName.Text;
            //    model.ClientId = orderModel.ClientId;
            //    model.DateAndTime = DateTime.Now.ToString();
            //    model.Money = -money;
            //    model.MoneyType = (int)BLL.Shop.MoneyType.PayByBalance;
            //    model.CurrencyType = (int)BLL.Shop.CurrencyType.RMB;
            //    model.EBankID = 0;
            //    model.Bank = string.Empty;
            //    model.Income_Payout = (int)BLL.Shop.Income_Payout.Payout;
            //    model.ID = orderModel.ID;
            //    model.PaymentId = 0;
            //    model.Remark = "支付订单费用，订单号：" + orderModel.OrderNum + "  " + txtAddPayment_remark.Text;
            //    model.LogTime = DateTime.Now.ToString();
            //    model.Inputer = AdminName;
            //    model.Ip = WebUtils.GetIP();

            //    string msg = string.Empty;
            //    if (bankroolItem.Add(model, out msg) > 0)
            //    {
            //        //更新订单信息

            //        orderModel.MoneyReceived = money + orderModel.MoneyReceived;
            //        if (orderModel.MoneyReceived > 0)
            //        {
            //            orderModel.Status = (int)WinnerCMS.BLL.Shop.Status.ReceivedEarnest;
            //            if (orderModel.MoneyReceived == orderModel.MoneyTotal)
            //                orderModel.Status = (int)WinnerCMS.BLL.Shop.Status.Payoff;
            //        }

            //        order.Update(orderModel);

            //        //更新用户信息
            //        userModel.Balance = userModel.Balance - money;
            //        user.Update(userModel);

            //        //提示成功信息

            //        msg = "操作成功！";
            //        MsgBox.Alert(msg, UpdatePanel1);

            //        ViewShowOrderItemDataBind(orderModel.ID);
            //        multiViewOrder.SetActiveView(viewShowOrderItem);
            //    }
            //    else
            //    {
            //        msg = "系统错误发生错误，请重新汇款！";
            //        MsgBox.Alert(msg, UpdatePanel1);
            //    }
            //}
        }

        #endregion

        #region  ViewInvoice

        //开具发票视图数据绑定
        protected void ViewInvoiceDataBind(int orderId)
        {
            var order = new OrdersManage();

            var model = order.GetInfo(orderId);
            if (model != null)
            {
                this.lblOrderId15.Text = orderId.ToString();
                this.lblContacterName15.Text = model.ContacterName;
                this.lblUserName15.Text = model.UserName;
                this.lblMoneyTotal15.Text = string.Format("{0:c}", model.MoneyTotal);
                this.lblMoneyReceived15.Text = string.Format("{0:c}", model.MoneyReceived);
                this.lblOrderNum15.Text = model.OrderNum;
                this.lblInvoiceContent15.Text = model.InvoiceContent;
                this.lblTotalMoneyMsg15.Text = string.Empty;
                this.lblInvoiceDateMsg15.Text = string.Empty;

                this.txtTotalMoney15.Text = model.MoneyTotal.ToString();
                this.txtInvoiceDate15.Text = DateTime.Now.ToString("yyyy-MM-dd");
                this.txtInvoiceContent15.Text = HttpUtility.HtmlDecode(model.InvoiceContent).Replace("<br>", "\r\n");

                this.multiViewOrder.SetActiveView(this.viewInvoice);
            }
        }

        //录入发票信息到InvoiceManage表
        protected void btnInvoiceConfirm_Click(object sender, EventArgs e)
        {
            //检查输入

            DateTime invoiceDate;
            decimal totalMoney = 0;

            var isErr = false;
            if (!decimal.TryParse(this.txtTotalMoney15.Text.Trim(), out totalMoney))
            {
                isErr = true;
                this.lblTotalMoneyMsg15.Text = "金额输入错误！";
            }
            if (!DateTime.TryParse(this.txtInvoiceDate15.Text.Trim(), out invoiceDate))
            {
                isErr = true;
                this.lblInvoiceDateMsg15.Text = "日期格式不对！";
            }
            if (isErr)
                return;

            //录入发票信息
            var invoiceItem = new InvoiceManage();
            var model = new WinnerCMS.Model.Invoice();
            var order = new OrdersManage();
            var orderModel = order.GetInfo(MyConvert.ToInt32(this.lblOrderId15.Text));
            if (orderModel != null)
            {
                model.UserName = orderModel.UserName;
                model.OrderId = orderModel.Id;
                model.InvoiceType = MyConvert.ToInt32(this.drpInvoiceType15.SelectedValue);
                model.InvoiceNum = this.txtInvoiceNum15.Text.Trim();
                model.InvoiceTitle = this.txtInvoiceTitle15.Text.Trim();
                model.InvoiceContent = HttpUtility.HtmlEncode(this.txtInvoiceContent15.Text.Trim());
                model.InvoiceDate = invoiceDate;
                model.TotalMoney = totalMoney;
                model.Drawer = this.txtDrawer15.Text.Trim();
                model.Admin = UserManage.Current.UserName;
                model.CheckInDate = DateTime.Now;

                var resu = invoiceItem.Add(model);

                if (string.IsNullOrEmpty(resu.Errors))
                {
                    //更新订单信息

                    orderModel.Invoiced = true;
                    var o = order.Add(orderModel, null);
                    if (string.IsNullOrEmpty(o.Errors) == false)
                    {
                        MsgBox.Message(o.Errors, this.UpdatePanel1);
                        return;
                    }

                    //提示成功信息


                    MsgBox.Message("操作成功！", this.UpdatePanel1);

                    ViewShowOrderItemDataBind(orderModel.Id);
                    this.multiViewOrder.SetActiveView(this.viewShowOrderItem);
                }
                else
                {
                    MsgBox.Message("系统错误发生错误，请重新开具发票！", this.UpdatePanel1);
                }
            }
        }

        #endregion
    }
}